package
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;

	public class FGGameManager
	{
		private var renderer:FGRenderer;
		private var stg:Stage;
		private var user:IFGFighter;
		private var pieces:Vector.<IFGPiece>;
		private var DEFAULT_USER_X:int = 150;
		private var DEFAULT_USER_Y:int = 250;
		private var USER_MOVEMENT_SPEED:int = 3;
		
		public function FGGameManager(stage:Stage, renderer:FGRenderer)
		{
 			this.stg = stage;
			this.renderer = renderer;
			pieces = new Vector.<IFGPiece>();
			user = buildUserFighter();
			buildDrone(0,10,2,3,40,10,120);
			buildDrone(100,50,2,0,60,1,600);
			stg.addEventListener(KeyboardEvent.KEY_DOWN, start);
		}
		
		private function keyDown(event:KeyboardEvent):void {
			//track: up, down, left, right, space, enter
			switch (event.keyCode) {
				case 37: //left
					user.setXVel(-USER_MOVEMENT_SPEED);
					break;
				case 38: //up
					user.setYVel(-USER_MOVEMENT_SPEED);
					break;
				case 39: //right
					user.setXVel(USER_MOVEMENT_SPEED);
					break;
				case 40: //down
					user.setYVel(USER_MOVEMENT_SPEED);
					break;
				case 32: //space
					var shot:IFGPiece = user.shoot();
					var spt:Sprite = shot.getSprite();
					stg.addChild(spt);
					pieces.push(shot);
					break;
				case 13: //enter
					pause();
					break;
				default:
					break; 
			}
		}
		
		private function keyUp(event:KeyboardEvent):void {
			//If there are 'sticky' movement errors, check here. 
			switch (event.keyCode) {
				case 37: //left
					if (user.getXVel() == -USER_MOVEMENT_SPEED) 
						user.setXVel(0);
					break;
				case 38: //up
					if (user.getYVel() == -USER_MOVEMENT_SPEED)
						user.setYVel(0);
					break;
				case 39: //right
					if (user.getXVel() == USER_MOVEMENT_SPEED)
						user.setXVel(0);
					break;
				case 40: //down
					if (user.getYVel() == USER_MOVEMENT_SPEED) 
						user.setYVel(0);
					break;
				default:
					break;
			}
		}
		
		private function pause():void {
			stg.removeEventListener(Event.ENTER_FRAME, updateGameState);
			stg.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			stg.addEventListener(KeyboardEvent.KEY_DOWN, start);
		}
		
		private function start(event:KeyboardEvent):void {			
			stg.removeEventListener(KeyboardEvent.KEY_DOWN, start);
			stg.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			stg.addEventListener(KeyboardEvent.KEY_UP, keyUp);			
			stg.addEventListener(Event.ENTER_FRAME, updateGameState);
		}
		
		private function updateGameState(event:Event):void {			
			for each (var piece:IFGPiece in pieces) {
				piece.move();
				//trace(piece.getXPos()+" " +piece.getYPos());
			}
			//checkForCollisions();
			//checkForGameEvents();
			//renderer.renderGameState(pieces);
			
		}

		
		private function checkForCollisions():void {
			//hittestobject thingy
			//FOR EACH LOOP!
		}
		
		private function checkForGameEvents():void {
			
		}
		
		private function buildUserFighter():IFGFighter {
			var user:IFGFighter = new UserFighter(DEFAULT_USER_X, DEFAULT_USER_Y, 0, 0, new Blaster());
			stg.addChild(user.getSprite());
			pieces.push(user);
			return user;
		}
		
		private function buildDrone(xPos:int, yPos:int, xVel:int, yVel:int, xCycleLength:int, yCycleLength:int, shotFrequency:int):IFGFighter {
			var drone:IFGFighter = new Drone(xPos, yPos, xVel, yVel, new Blaster(), xCycleLength, yCycleLength, shotFrequency);
			stg.addChild(drone.getSprite());
			pieces.push(drone);
			return drone;
		}
	}
}